package com.sc.nft.dto;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sc.nft.enums.EcologyIncomeTypeEnum;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.EcologyHatchIncomeConf;
import com.sc.nft.mapper.EcologyHatchIncomeConfMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 生态-收益-配置(sc_ecology_hatch_income_conf)数据DAO
 *
 * @author Sun
 * @since 2023-10-13 17:22:11
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class EcologyHatchIncomeConfDao extends ServiceImpl<EcologyHatchIncomeConfMapper, EcologyHatchIncomeConf> {

    public List<EcologyHatchIncomeConf> getAdminIncomeConf() {
        LambdaQueryWrapper<EcologyHatchIncomeConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(EcologyHatchIncomeConf::getIncomeType, EcologyIncomeTypeEnum.RECOMMENDED_INCOME);
        return list(wrapper);
    }

    public List<EcologyHatchIncomeConf> getAdminIncomeConfByRelevanceId(Long relevanceId) {
        LambdaQueryWrapper<EcologyHatchIncomeConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(EcologyHatchIncomeConf::getIncomeType, EcologyIncomeTypeEnum.RECOMMENDED_INCOME);
        wrapper.eq(EcologyHatchIncomeConf::getConfRelevanceId, relevanceId);
        return list(wrapper);
    }

    public boolean updateIncomeScaleById(Long id, BigDecimal incomeScale) {
        LambdaUpdateWrapper<EcologyHatchIncomeConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EcologyHatchIncomeConf::getId, id);
        wrapper.set(EcologyHatchIncomeConf::getIncomeScale, incomeScale);
        return update(new EcologyHatchIncomeConf(),wrapper);
    }

    public EcologyHatchIncomeConf getOneByType(EcologyIncomeTypeEnum incomeType) {
        LambdaQueryWrapper<EcologyHatchIncomeConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchIncomeConf::getIncomeType, incomeType);
        wrapper.last("order by id desc limit 1 ");
        return getOne(wrapper);

    }

    public BigDecimal getSumAmountIncomeByDaoIdAndTypes(Long daoId,List<EcologyIncomeTypeEnum> typeList) {
        List<Integer> types = typeList.stream().map(EcologyIncomeTypeEnum::getCode).collect(Collectors.toList());
        return baseMapper.getSumAmountIncomeByDaoIdAndTypes(daoId,types);
    }

    public Boolean removeByRelevanceId(Long relevanceId) {
        LambdaUpdateWrapper<EcologyHatchIncomeConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EcologyHatchIncomeConf::getConfRelevanceId, relevanceId);
        return remove(wrapper);
    }


    public EcologyHatchIncomeConf getAdminIncomeConfByRelevanceIdAndType(Long relevanceId ,EcologyIncomeTypeEnum type) {
        LambdaQueryWrapper<EcologyHatchIncomeConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchIncomeConf::getConfRelevanceId, relevanceId);
        wrapper.eq(EcologyHatchIncomeConf::getIncomeType, type);
        wrapper.last("order by id desc limit 1");
        return getOne(wrapper);
    }
}